/*
 * This file is part of "Deathwatch Ammo Counter for Android".
 *
 * "Deathwatch Ammo Counter for Android" is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 *
 * "Deathwatch Ammo Counter for Android" is distributed in the hope that it 
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with "Deathwatch Ammo Counter for Android".  
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package com.TikiiTorch.Deathwatch.AmmoCounter.file;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


import android.content.Context;
import android.content.res.Resources;

import com.TikiiTorch.Deathwatch.AmmoCounter.R;
import com.TikiiTorch.Deathwatch.AmmoCounter.Weapon;

public class WeaponHandler extends DefaultHandler {

	// ArrayList<Weapon> xmlMatches = null;

	Weapon weapon;

	boolean bWeap = false;
	boolean bName = false;
	boolean bROF = false;
	boolean bType = false;
	boolean bClipSize = false;
	boolean bGroup = false;

	private static String[] weaponGroups;
	private String[] weaponGroupsShort;
	private ArrayList<Weapon>[] weapons;

	@SuppressWarnings("unchecked")
	public WeaponHandler(Context context) {

		Resources res = context.getResources();
		
		weaponGroups = res.getStringArray(R.array.weaponGroups);
		weaponGroupsShort = res.getStringArray(R.array.weaponGroupsShort);
		
		weapons = new ArrayList[weaponGroups.length];
		for (int i = 0; i < weapons.length; i++)
			weapons[i] = new ArrayList<Weapon>();
	}

	public static String[] getWeaponGroups(Context context) {
		
		Resources res = context.getResources();
		
		return res.getStringArray(R.array.weaponGroups);
		
	}

	public Weapon[][] getWeapons() {
		Weapon res[][] = new Weapon[weapons.length][];
		for (int i = 0; i < weapons.length; i++) {
			res[i] = new Weapon[weapons[i].size()];
			for (int j = 0; j < weapons[i].size(); j++) {
				res[i][j] = weapons[i].get(j);
			}
		}
		return res;
	}

	private int findGroupIndex(String groupName) {
		for (int i = 0; i < weaponGroupsShort.length; i++) {
			if (weaponGroupsShort[i].equalsIgnoreCase(groupName)) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public void startDocument() throws SAXException {
		// some sort of setting up work
	}

	@Override
	public void endDocument() throws SAXException {
		// some sort of finishing up work
	}

	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (qName.equalsIgnoreCase("weapon")) {
			weapon = new Weapon();
			bWeap = true;
		}
		if (bWeap) {
			if (qName.equalsIgnoreCase("weaponName")) {
				bName = true;
			}
			if (qName.equalsIgnoreCase("rof")) {
				bROF = true;
			}
			if (qName.equalsIgnoreCase("weaponType")) {
				bType = true;
			}
			if (qName.equalsIgnoreCase("clipSize")) {
				bClipSize = true;
			}
			if (qName.equalsIgnoreCase("weapongroup")) {
				bGroup = true;
			}
		}
	}

	@Override
	public void characters(char ch[], int start, int length) {

		if (bWeap) {
			if (bName) {
				weapon.setName(new String(ch, start, length));
				bName = false;
			}
			if (bROF) {
				// parse for weapon rate of fire
				int[] weapRof = new int[3];
				String sRof = new String(ch, start, length);
				String[] rof = sRof.split(",");
				for (int i = 0; i < weapRof.length; i++)
					weapRof[i] = Integer.parseInt(rof[i]);
				weapon.setROF(weapRof);
				bROF = false;
			}
			if (bType) {
				weapon.setAmmoType(new String(ch, start, length));
				bType = false;
			}
			if (bClipSize) {
				weapon.setClipSize(Integer.parseInt(new String(ch, start,
						length)));
				bClipSize = false;
			}
			if (bGroup) {
				weapon.setGroup(new String(ch, start, length));

				bGroup = false;
			}

		}

	}

	/**
	 * Occurs when the parser reaches the end of an element
	 */
	@Override
	public void endElement(String namesaceURI, String localName, String qName)
			throws SAXException {
		if (qName.equalsIgnoreCase("weapon")) {
			// end of a weapon node
			// xmlMatches.add(weapon);
			int index = findGroupIndex(weapon.getGroup());
			if (index > -1) {
				weapons[index].add(weapon);
			}
			bWeap = false;
		}
		if (bWeap) {
			if (qName.equalsIgnoreCase("weaponName")) {
				bName = false;
			}
			if (qName.equalsIgnoreCase("rof")) {
				bROF = false;
			}
			if (qName.equalsIgnoreCase("weaponType")) {
				bType = false;
			}
			if (qName.equalsIgnoreCase("clipSize")) {
				bClipSize = false;
			}
			if (qName.equalsIgnoreCase("weapongroup")) {
				bGroup = false;
			}
		}
	}

}
